<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\adminapi\controller\v1\platform\work_wechat;


use app\adminapi\controller\v1\platform\PlatformController;
use app\common\model\wework\WeworkApps;
use FormBuilder\Driver\CustomComponent;
use hiduoke\services\FormBuilder as Form;
use think\App;

class WorkWechatApps extends PlatformController
{

    /**
     * @var WeworkApps
     */
    private WeworkApps $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new WeworkApps();
    }

    /**
     * 显示资源列表
     * @Route("WechatUser", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        $res = $this->model->getPageList();
        return $this->success($res['count'], $res['data']);
    }

    //创建产品
    public function createForm()
    {
        $rule = array();
        $id = input('id', 0);
        $row = $this->model->findOrEmpty($id);//创建表单
        $form = Form::createForm("/adminapi/platform/work_wechat/apps/save?id=" . $id)->setMethod("post");
        if (!$id) {
            $url = "请先保存后再编辑，即可复制事件地址";
        } else {
            $url = url(config('hiduoke.work_wechat_app') . "/event/apps", ['p' => get_plaid(), 'app_id' => $id], '', true)->build();
        }

        $rule[] = Form::ElTabs()->appendChild(
            Form::ElTabPane()->props(['label' => "基础设置", "name" => "base"])->appendChildren([
                Form::hidden('plaid', get_plaid()),
                Form::dkInput('name', '应用名称', $row['name'] ?? '')->required(),
                Form::dkImage('logo', '应用图标', $row['logo'] ?? '')->required(),
                Form::dkInput('agent_id', 'AgentId', $row['agent_id'] ?? '', '在应用管理-自建应用-AgentId')->required(),
                Form::dkInput('config_secret', 'Secret', $row['config']['secret'] ?? '', "填写应用Secret，在应用管理-自建应用-Secret-查看")->required(),
            ])
        )->appendChild(
            Form::ElTabPane()->props(['label' => "接收事件服务器", "name" => "event"])->appendChildren([
                Form::dkCopyInput("url", 'URL', $url)
                    ->appendRule("suffix", Form::getHelp("将当前地址填写至事件服务器URL，在应用管理-自建应用-接收消息-设置API接收-URL"))
                    ->disabled(true),

                Form::input('config_token', 'Token', $row['config']['token'] ?? '')
                    ->appendRule("suffix", Form::getHelp("服务端Token，在应用管理-自建应用-接收消息-设置API接收-Token"))
                    ->required(),
                Form::input('config_aes_key', 'AESKey', $row['config']['aes_key'] ?? '')
                    ->appendRule("suffix", Form::getHelp("服务端EncodingAESKey，在应用管理-自建应用-接收消息-设置API接收-EncodingAESKey"))
                    ->required(),
            ])
        )->props(['value' => 'base']);
        $title = !$id ? "创建自建应用" : "编辑自建应用";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form), $row);
    }

    public function save()
    {
        $id = input('id', 0);
        $row = $this->model->findOrEmpty($id);
        $formData = getFormData();
        $row->save($formData);
        if ($id) {
            return $this->success("保存成功");
        } else {
            return $this->success('新增成功');
        }
    }

    public function setDefault($id)
    {
        $row = $this->model->findOrEmpty($id);
        (new WeworkApps())->where('plaid', get_plaid())->update(['is_default' => 0]);
        $row->save(['is_default' => 1]);
        return $this->success('设置成功');
    }
}
